package com.atguigu.guli.service.trade.controller;


import com.atguigu.commonutils.JwtInfo;
import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.result.R;
import com.atguigu.guli.service.trade.entity.Order;
import com.atguigu.guli.service.trade.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author wangyang
 * @since 2021-06-15
 */
@Api(tags = "网站订单管理")
@Slf4j
@CrossOrigin //使用网关访问,配置了跨域设置
@RestController
@RequestMapping("/trade/order")
public class OrderController {

    @Autowired
    private OrderService orderService;


    @ApiOperation("新增订单")
    @PostMapping("auth/save/{courseId}")
    public R saveOrderByCourseId(@RequestParam String courseId,HttpServletRequest request) {
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(request);
        String orderId = orderService.saveOrderByCourseId(courseId,jwtInfo);
        return R.ok().message("保存成功!").data("orderId",orderId);
    }


    @ApiOperation("通过订单Id获取订单")
    @GetMapping("auth/get/{orderId}")
    public R getOrderById(
            @PathVariable String orderId, HttpServletRequest request
            ) {
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(request);
       Order order = orderService.getOrderById(orderId,jwtInfo);
       return R.ok().data("order",order);
    }

    @ApiOperation( "判断课程是否购买")
    @GetMapping("auth/is-buy/{courseId}")
    public R isBuyByCourseId(@PathVariable String courseId, HttpServletRequest request) {
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(request);
        Boolean isBuy = orderService.isBuyByCourseId(courseId, jwtInfo.getId());
        return R.ok().data("isBuy", isBuy);
    }

    @ApiOperation(value = "获取当前用户订单列表")
    @GetMapping("auth/list")
    public R list(HttpServletRequest request) {
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(request);
        /*Map<String,Object> map = new HashMap<>();
        map.put("member_id",jwtInfo.getId());
        List<Order> orderList = orderService.listByMap(map);*/

        //需要更跟据时间排序
        List<Order> orderList = orderService.listByMemberId(jwtInfo.getId());
        return R.ok().data("orderList",orderList);
    }

    @ApiOperation(value = "删除订单")
    @DeleteMapping("auth/remove/{orderId}")
    public R remove(@PathVariable String orderId, HttpServletRequest request) {
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(request);
        Boolean result =  orderService.removeByOrderId(orderId,jwtInfo.getId());
        if (result) {
            return R.ok().message("删除成功！");
        }else {
            return  R.error().message("删除失败！");
        }
    }
}

